package Windows.Share;

import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

import GroupWare.ShareFile;
import Tools.IMG;
import Tools.InfoFormat;
import Tools.Units;
import Windows.MainWindows;

public class ResShareTableModel extends AbstractTableModel {

	private String[] columnNames = { "图标", "资源名称", "类型", "评论", "文件大小", "资源", "更新日期", "MD5" };

	private ShareFile sfDir = MainWindows.getInstance().client.sp;// 当前表格目录
	private JTable table;

	public ResShareTableModel(JTable table) {
		this.table = table;
		table.setModel(this);
	}

	@Override
	public Class<?> getColumnClass(int columnIndex) {
		if (columnIndex == 0) {
			return ImageIcon.class;
		}
		return String.class;// 所有列都是字符
	}

	@Override
	public int getColumnCount() {
		return columnNames.length;
	}

	@Override
	public String getColumnName(int columnIndex) {
		return columnNames[columnIndex];
	}

	public ShareFile getFile(int index) {
		return new ShareFile(sfDir.getF().listFiles()[index]);
	}

	@Override
	public int getRowCount() {
		File[] fs = sfDir.getF().listFiles();
		if (fs == null)
			return 0;
		return fs.length;
	}

	public ShareFile getSfDir() {

		return sfDir;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		ShareFile sf = sfDir.getChildShareFile(rowIndex);// 按照索引取当前文件夹下的文件
		boolean isfile = sf.getF().isFile();
		switch (columnIndex) {
		case 0:
			return !isfile ? IMG.iid : IMG.iif;
		case 1:
			return sf.getF().getName();
		case 2:
			return isfile ? InfoFormat.getTypeStr(sf.getF().getName()) : "文件夹";
		case 3:
			return isfile ? "(256)" : "";
		case 4:
			return isfile ? Units.byteCondense(sf.getF().length()) : "";
		case 5:
			return isfile ? "1024" : "";
		case 6:
			return InfoFormat.formatTime(sf.getF().lastModified());
		case 7:
			return isfile ? sf.md5 : "";
		default:
			return "";
		}
	}

	public void setSfDir(ShareFile sfDir) {
		this.sfDir = sfDir;
		table.updateUI();
		System.out.println("目录改变:" + sfDir);
	}
	// public xmlFlag getFileInfoXml(int row){
	// xmlFlag xf = null;
	// ShareFile sf = getFile(row);
	// if(sf!=null){
	// xf = new xmlFlag(null,"File");
	// xf.put("size", value)
	// }
	// }
}
